#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>
 
using namespace std;
using LL = long long;
using PII = pair<long, long>;
const int N = 1e5 + 10;
 
int n, tt1, tt2;
LL a[N], b[N];
LL f1[N], f2[N];
PII d[N];
int T;
int jj = 1;
 
void solve(){
    tt1 = 0, tt2 = 0;
    cin >> n;
    for(int i = 0; i < n; i ++) cin >> a[i];
    for(int i = 0; i < n; i ++) cin >> b[i];
 
    for(int i = 0; i < n; i ++){
        d[i] = {b[i], a[i]};
    }
    sort(d, d + n);
 
    for(int i = 0; i < n; i ++){
        if(d[i].second == 0){
            f1[tt1++] = d[i].first;
        }else{
            f2[tt2++] = d[i].first;
        }
    }

    int tt11 = tt1, tt22 = tt2;
 
    LL res = 0;
    LL ftype = 1;

    LL fki = 0;
    while(true){
        if(tt1 == 0 && tt2 == 0) break;
 
        if(ftype){ //fa: frost
            if(tt1 != 0){
                res = res + fki;
                res = res + f1[tt1 - 1];
                fki = f1[tt1 - 1];
                ftype = 0;
                tt1 --;
            }else{
                res = res + f2[tt2 - 1];
                fki = f2[tt2 - 1];
                ftype = 1;
                tt2 --;
            }
        }else{ // fa: fire
            if(tt2 != 0){
                res = res + fki;
                res = res + f2[tt2 - 1];
                fki = f2[tt2 - 1];
                ftype = 1;
                tt2 --;
            }else{
                res = res + f1[tt1 - 1];
                fki = f1[tt1 - 1];
                ftype = 0;
                tt1 --;
            }
        }
    }


    LL res1 = res;
    res = 0;
    ftype = 0;
    res = 0;
    fki = 0;
    tt1 = tt11, tt2 = tt22;
    while(true){
        if(tt1 == 0 && tt2 == 0) break;
 
        if(ftype){ //fa: frost
            if(tt1 != 0){
                res = res + fki;
                res = res + f1[tt1 - 1];
                fki = f1[tt1 - 1];
                ftype = 0;
                tt1 --;
            }else{
                res = res + f2[tt2 - 1];
                fki = f2[tt2 - 1];
                ftype = 1;
                tt2 --;
            }
        }else{ // fa: fire
            if(tt2 != 0){
                res = res + fki;
                res = res + f2[tt2 - 1];
                fki = f2[tt2 - 1];
                ftype = 1;
                tt2 --;
            }else{
                res = res + f1[tt1 - 1];
                fki = f1[tt1 - 1];
                ftype = 0;
                tt1 --;
            }
        }
    }

 
    cout << max(res1, res) << '\n';
}
 
int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    
    cin >> T;
    while(jj <= T){
        solve();
        jj ++;
    }
    return 0;
}